package camid.infrastructure;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import camid.distribution.protocol.MethodRequestMessage;
import camid.distribution.protocol.MethodResponseMessage;


public class SenderQueue {

//	private static SenderQueue INSTANCE;
	
	private ExecutorService executor;
	
	public SenderQueue()
	{
		executor = Executors.newCachedThreadPool();
	}
	
//	public static synchronized SenderQueue getInstance()
//	{
//		if(INSTANCE == null)
//		{
//			INSTANCE = new SenderQueue();
//		}
//		return INSTANCE;
//	}
	
	public MethodResponseMessage doSend(Connector sender, MethodRequestMessage request)
	{
		SenderTask task = new SenderTask(sender, request);
		Future<MethodResponseMessage> futureMsg = executor.submit(task);
		MethodResponseMessage response = null;
		try {
			response = futureMsg.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}
	
	public void shutdown()
	{
		executor.shutdown();
//		INSTANCE = null;
	}
	
}
